//
//  MBPhoneViewController.m
//  Meboo
//
//  Created by kyo on 2023/11/21.
//

#import "MBPhoneViewController.h"
#import "MBPhoneCodeViewController.h"
#import "MBPhoneNumView.h"
#import "FirebaseAuth/FIRPhoneAuthProvider.h"
@interface MBPhoneViewController ()
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UILabel *phoneLab;
@property (nonatomic, strong) UILabel *noticeLab;
@property (nonatomic, strong) MBPhoneNumView *phoneView;
@property (nonatomic, strong) UIActivityIndicatorView *stateView;
@property (nonatomic, copy) NSString *phoneStr;

@property (nonatomic, strong) UIButton *nextBtn;

@end

@implementation MBPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    [self setupUI];
}

#pragma mark - Action

- (void)leftAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)nextAction {
    [self.stateView startAnimating];
    self.nextBtn.enabled = NO;
    [[FIRPhoneAuthProvider provider] verifyPhoneNumber:[NSString stringWithFormat:@"+1%@",self.phoneStr]
                                            UIDelegate:nil
                                            completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) {
        self.nextBtn.enabled = YES;
        [self.stateView stopAnimating];
      if (error) {
//        [self showMessagePrompt:error.localizedDescription];
          NSLog(@"%@",error.localizedDescription);
        return;
      }
      // Sign in using the verificationID and the code sent to the user
      // ...
        NSLog(@"%@",verificationID);
        MBPhoneCodeViewController *phoneCodeVc = [MBPhoneCodeViewController new];
        phoneCodeVc.phoneStr = self.phoneStr;
        phoneCodeVc.verificationID = verificationID;
        [self.navigationController pushViewController:phoneCodeVc animated:YES];
        
//        FIRAuthCredential *credential = [[FIRPhoneAuthProvider provider]
//            credentialWithVerificationID:verificationID
//                        verificationCode:userInput];
        
    }];
//    [[MBLoginDataManager sharedManager] getPhoneCodeWithPhone:[NSString stringWithFormat:@"1%@",self.phoneStr] success:^(BOOL success) {
//
//    }];
}

- (NSString *)removeSpaceWithString:(NSString *)string {
    //去除开头和结尾的空字符串
    NSCharacterSet *set = [NSCharacterSet whitespaceCharacterSet];
    NSString *trimmedString = [string stringByTrimmingCharactersInSet:set]; // 去除空格后的字符串
    NSLog(@"%@", trimmedString);
    return trimmedString;
}

#pragma mark - UI
- (void)setupUI {
    
    [self.view addSubview:self.leftBtn];
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(15));
        make.top.mas_equalTo(StatusBarAndNavigationBarHeight-MBHeightScale(13)-MBHeightScale(20));
        make.height.width.mas_equalTo(MBHeightScale(20));
    }];
    
    [self.view addSubview:self.phoneLab];
    [self.phoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.left.mas_equalTo(MBWidthScale(30));
        make.right.mas_equalTo(MBWidthScale(-30));
        make.top.mas_equalTo(MBHeightScale(10)+StatusBarAndNavigationBarHeight);
    }];
    
    [self.view addSubview:self.phoneView];
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneLab.mas_bottom).offset(MBHeightScale(42));
        make.right.mas_equalTo(MBWidthScale(-30));
        make.left.mas_equalTo(MBWidthScale(30));
        make.height.mas_equalTo(MBHeightScale(40));
    }];
    
    [self.view addSubview:self.noticeLab];
    [self.noticeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(30));
        make.right.mas_equalTo(MBWidthScale(-30));
        make.top.equalTo(self.phoneView.mas_bottom).offset(20);
    }];
    
    [self.view addSubview:self.nextBtn];
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(30));
        make.right.mas_equalTo(MBWidthScale(-30));
        make.top.equalTo(self.noticeLab.mas_bottom).offset(80);
        make.height.mas_equalTo(MBHeightScale(56));
    }];
    
    [self.view addSubview:self.stateView];
    [self.stateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view.mas_top).offset(ScreenHeight/3);
    }];
}

#pragma mark - Setter && Getter

- (UIButton *)leftBtn {
    if (!_leftBtn) {
        _leftBtn = [[UIButton alloc] init];
        [_leftBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_leftBtn setImage:ImageWithName(@"login_back") forState:0];
        [_leftBtn addTarget:self action:@selector(leftAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftBtn;
}

- (UILabel *)phoneLab {
    if (!_phoneLab) {
        _phoneLab = [[UILabel alloc] init];
        _phoneLab.font = MBHelBoldFontSizeScale(28);
        _phoneLab.numberOfLines = 0;
        _phoneLab.textColor = HEXS(@"333333");
        _phoneLab.text = @"What’s your number";
    }
    return _phoneLab;
}

- (UILabel *)noticeLab {
    if (!_noticeLab) {
        _noticeLab = [[UILabel alloc] init];
        _noticeLab.font = MBHelFontSizeScale(12);
        _noticeLab.numberOfLines = 0;
        _noticeLab.textColor = HEXS(@"202323");
        _noticeLab.text = @"We never share this with anyone and will not appear on your profile information.";
    }
    return _noticeLab;
}

- (MBPhoneNumView *)phoneView {
    if (!_phoneView) {
        _phoneView = [[MBPhoneNumView alloc] init];
        __WeakSelf
        _phoneView.phoneBlock = ^(NSString * _Nonnull phoneNum) {
            weakSelf.phoneStr = phoneNum;
            if (phoneNum.length >= 10) {
                [weakSelf.nextBtn setTitleColor:UIColor.whiteColor forState:0];
                [weakSelf.nextBtn setBackgroundColor:HEXS(@"22253C")];
                weakSelf.nextBtn.enabled = YES;
            } else {
                [weakSelf.nextBtn setTitleColor:HEXS(@"BBBBBB") forState:0];
                [weakSelf.nextBtn setBackgroundColor:HEXS(@"F5F7F9")];
                weakSelf.nextBtn.enabled = NO;
            }
        };
    }
    return _phoneView;
}

- (UIButton *)nextBtn {
    if (!_nextBtn) {
        _nextBtn = [[UIButton alloc] init];
        [_nextBtn setTitle:@"Next" forState:0];
        _nextBtn.layer.cornerRadius = MBHeightScale(56/2);
        _nextBtn.titleLabel.font = MBHelFontSizeScale(16);
//        [_nextBtn setTitleColor:UIColor.whiteColor forState:0];
//        [_nextBtn setBackgroundColor:HEXS(@"22253C")];
        [_nextBtn setTitleColor:HEXS(@"BBBBBB") forState:0];
        [_nextBtn setBackgroundColor:HEXS(@"F5F7F9")];
        _nextBtn.enabled = NO;
        [_nextBtn addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextBtn;
}

- (UIActivityIndicatorView *)stateView {
    if (!_stateView) {
        _stateView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleLarge)];
    }
    return _stateView;
}
@end
